<template>
	<view class="container">
		<image :src="testData.imageUrl" mode="aspectFit" class="product-img"></image>
		<text class="product-name">购买1件{{testData.name}}</text>
		<text class="product-price">{{testData.price}}元</text>
		<button class="pay-btn" @click="onWechatPayButtonClicked()">
			<image src="/static/wechat-icon.png" class="wechat-icon"></image>
			<text class="pay-btn-text">微信支付</text>
		</button>
	</view>
</template>

<script>
	let tag = 'purchase'

	export default {
		data() {
			return {
				testData: ""
			}
		},
		methods: {
			onWechatPayButtonClicked() {
				this.$log.info(tag, "onWechatPayButtonClicked");
				this.createOrder();
			},

			createOrder() {
				this.$log.info(tag, "createOrder");

				uni.request({
					url: `${this.constant.BASE_URL}/api/order/create4Ma`,
					method: 'POST',
					header: {
						'Authorization': this.loginManager.getToken(),
					},
					data: {
						'amount': 1,
						'machineNo': this.constant.MACHINE_NO,
						'productId': this.testData.id,
					},
					success: (res) => {
						this.$log.info(tag, JSON.stringify(res.data))
						this.$log.info(tag, `createOrder success ${res.data.code}`)
						if (res.data.code == 200) {
							this.$log.info(tag, "创建成功")
							this.payOrder(res.data.orderNo);
						}
					},
					fail: (error) => {
						console.error(error);
					}
				})
			},
			payOrder(orderNo) {
				this.$log.info(tag, `payOrder ${orderNo}`)
				uni.request({
					url: `${this.constant.BASE_URL}/wxpay/notify/pay/${orderNo}`,
					method: 'GET',
					success: (res) => {
						this.$log.info(tag, "success")
						this.$log.info(tag, JSON.stringify(res.data))
						if (res.data.code == 200) {
							uni.showToast({
									icon: 'success',
									title: `支付成功${orderNo}`,
									duration: 1500
								}),
								uni.navigateTo({
									url: `/pages/order/order?orderNo=${orderNo}`
								})
						}
					},
					fail: (err) => {
						this.$log.error(tag, err.errMsg)
					}
				})
			}

		},

		onLoad(parameters) {
			this.$log.info(tag, `onLoad`)
			let productJSONString = decodeURIComponent(parameters.product)
			let product = JSON.parse(productJSONString)
			this.testData = product
		},

		onShow() {
			this.$log.info(tag, `onShow`);
		},

		onReady() {
			this.$log.info(tag, `onReady`);
		},

		onHide() {
			this.$log.info(tag, `onHide`);
		},

		onUnload() {
			this.$log.info(tag, `onUnload`);
		},
	}
</script>

<style>
	.container {
		padding: 30rpx 20rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		background-color: #fafafa;
		min-height: 100vh;
	}

	.product-img {
		width: 320rpx;
		height: 320rpx;
		border-radius: 16rpx;
		margin-bottom: 30rpx;
	}

	.product-name {
		font-size: 34rpx;
		color: #333;
		font-weight: 500;
		margin-bottom: 20rpx;
	}

	.product-price {
		font-size: 40rpx;
		color: #ff3b30;
		font-weight: 600;
		margin-bottom: 40rpx;
	}

	.pay-btn {
		width: 90%;
		/* background-color: #07c160; */
		color: #fff;
		font-size: 32rpx;
		border-radius: 80rpx;
		padding: 18rpx 0;
		display: flex;
		align-items: center;
		justify-content: center;
		border: #999 solid;
	}

	.pay-btn::after {
		border: none;
	}

	.wechat-icon {
		width: 40rpx;
		height: 40rpx;
		margin-right: 16rpx;
		vertical-align: middle;
	}

	.pay-btn-text {
		vertical-align: middle;
		color: #333;
	}
</style>